import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
// import 'package:device_info/device_info.dart';

class Channel {
  static final Channel instance = Channel._interval();
  // factory Channel() => _getInstance();
  // static Channel get instance => _getInstance();
  // static Channel _instance = null;
  bool isMainProject = false;
  // static Channel _getInstance() {
  //   if (_instance == null) {
  //     _instance = Channel._interval();
  //   }
  //   return _instance;
  // }
  //提供了一个工厂方法来获取该类的实例
  factory Channel() {
    return instance;
  }

  void init() {
    Get.log("这里初始化");
  }

  late MethodChannel channel;
  Channel._interval() {
    init();
    Get.log('channel 初始化了');
    channel = const MethodChannel('com.flutter.call');
    channel.setMethodCallHandler((call) {
      if (call.method.startsWith('common_')) {
        ///公共方法前缀
      } else if (call.method == 'start_work') {
        accessToMainProject();
        Get.log('设置成功了');
      }
      return Future.error('未找到方法');
    });
  }

  void accessToMainProject() {
    isMainProject = true;
  }

  ///采集图片
  Future<bool> pickPhoto() async {
    var result = await channel.invokeMethod('common_pickPhoto');
    Get.log('flutter打印：$result');
    if (result == null) {
      return false;
    }
    return true;
  }

  ///弹窗
  Future<bool> alert(
      {String title = '',
      String msg = '',
      String cancelTitle = '取消',
      String sureTitle = '确定'}) async {
    var result = await channel.invokeMethod('common_pickPhoto', {
      'title': title,
      'msg': msg,
      'cancelTitle': cancelTitle,
      'sureTitle': sureTitle
    });
    Get.log('flutter打印：$result');
    if (result == null) {
      return false;
    }
    return true;
  }

  ///token 失效
  Future<bool> tokenValid() async {
    var result = await channel.invokeMethod('common_tokenValid');
    Get.log('flutter打印：$result');
    if (result == null) {
      return false;
    }
    return true;
  }

  ///账号封禁
  Future<bool> sealAccount() async {
    var result = await channel.invokeMethod('common_sealAccount');
    Get.log('flutter打印：$result');
    if (result == null) {
      return false;
    }
    return true;
  }

  ///nav返回
  Future systemNavBack() async {
    if (Platform.isIOS) {
      var result = await channel.invokeMethod('common_back', 'test');
      Get.log('flutter打印：$result');
    }
  }

  ///nav返回
  Future editUerInfo() async {
    if (Platform.isIOS) {
      var result = await channel.invokeMethod('userCenter_edit');
      Get.log('flutter打印：$result');
    }
  }
}
